#include <Controller.h>

Controller::Controller()
{
  image = "";
  finished = false;
}

void Controller::setImage(string imagePath)
{
  Lock sync(*this);
  image = imagePath;
}

void Controller::setFinished(bool f)
{
  Lock sync(*this);
  finished = f;
}

void Controller::setStatus(string renderer, string status)
{
  Lock sync(*this);
  for(int i = 0; i < renderers.size(); i++)
    {
      if(renderers.at(i).renderer == renderer){
	renderers.at(i).status = status;
	if(status == "RENDERING")
	  renderers.at(i).p2p = 100;
      }
    }
}

void Controller::incP2P(string renderer)
{
  Lock sync(*this);
  for(int i = 0; i < renderers.size(); i++)
    {
      if(renderers.at(i).renderer == renderer)
	renderers.at(i).p2p += 100/renderers.size();
    }
}

void Controller::setWorkUnit(string renderer, int workUnit)
{
  Lock sync(*this);
  for(int i = 0; i < renderers.size(); i++)
    {
      if(renderers.at(i).renderer == renderer){
	renderers.at(i).workUnit = workUnit;
      }
    } 
}

void Controller::setRenderer(string renderer)
{
  Lock sync(*this);
  info temp;
  temp.renderer = renderer;
  temp.status = "P2P";
  temp.p2p = 0;
  temp.workUnit = -1;
  renderers.push_back(temp);
}

string Controller::getImage(){
  Lock sync(*this);
  return image;
}

bool Controller::getFinished()
{
  Lock sync(*this);
  return finished;
}

vector<info> Controller::getRenderers()
{
  Lock sync(*this);
  return renderers;
}

void Controller::reset(){
  image = "";
  renderers.clear();
  finished = false;
}

Controller::~Controller(){
}
